package com.cssl.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.cssl.domain.DtsBrand;
import com.cssl.domain.dto.DtsBrandDto;
import com.cssl.mapper.DtsBrandMapper;
import com.cssl.service.DtsBrandService;
import com.cssl.util.InfoCode;
import com.cssl.util.ObjectMapperUtil;
import com.cssl.util.Result;
import com.cssl.util.StorageVariable;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class DtsBrandServiceImpl implements DtsBrandService {
    @Autowired
    private DtsBrandMapper mapper;
    @Resource
    private StringRedisTemplate template;
    @Resource
    private ObjectMapperUtil objectMapperUtil;

    @Override
    public List<DtsBrand> getAllBrand() {
        //先从redis中获取key:(brand:all)
        String s = template.opsForValue().get(StorageVariable.ALLBRAND);
        if (StrUtil.isEmpty(s)) {
            //为空
            List<DtsBrand> brand = mapper.getAllBrand();
            if (ObjectUtil.isEmpty(brand)) {
                return null;
            }
            template.opsForValue().set(StorageVariable.ALLBRAND, objectMapperUtil.toJsonString(brand), 7, TimeUnit.DAYS);
            return brand;
        }
        //不为空
        List<DtsBrand> dtsBrands = objectMapperUtil.fromJsonString(s, new TypeReference<List<DtsBrand>>() {
        });
        return dtsBrands;
    }

    @Override
    public Result getBrandById(Map map) {
        List<DtsBrand> brand = mapper.getBrandById(Integer.parseInt(map.get("id").toString()));
        if (ObjectUtil.isEmpty(brand)) {
            return Result.fail(InfoCode.QUERY_EMPTY, InfoCode.MESSAGE_QUERY_EMPTY, System.currentTimeMillis());
        }
        return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, brand, System.currentTimeMillis());
    }


    @Override
    public Map getAllBrandPage(Map map) {
        Map maps = new HashMap();
        Integer pages = Integer.parseInt(map.get("page").toString());
        Integer size = Integer.parseInt(map.get("size").toString());
        //先从redis中获取key:(brand:all:pages)
        String s = template.opsForValue().get(StorageVariable.ALLBRAND + ":" + pages);
        String totalPage = template.opsForValue().get(StorageVariable.ALLBRANDPAGE);
        if (StrUtil.isEmpty(s) && StrUtil.isEmpty(totalPage)) {
            //为空
            Page page = PageHelper.startPage(pages, size);
            mapper.getAllBrand();
            List<DtsBrand> brand = page.getResult();
            maps.put("brandList", brand);
            maps.put("page", page.getPages());
            if (ObjectUtil.isEmpty(brand)) {
                return null;
            }
            template.opsForValue().set(StorageVariable.ALLBRAND + ":" + pages, objectMapperUtil.toJsonString(brand), 7, TimeUnit.DAYS);
            template.opsForValue().set(StorageVariable.ALLBRANDPAGE, String.valueOf(page.getPages()), 7, TimeUnit.DAYS);
            return maps;
        }
        //不为空
        List<DtsBrand> dtsBrands = objectMapperUtil.fromJsonString(s, new TypeReference<List<DtsBrand>>() {
        });
        maps.put("brandList", dtsBrands);
        maps.put("page", totalPage);
        return maps;
    }

    @Override
    public Map findPagebrand(DtsBrandDto dtsBrandDto) {
        System.out.println("传入" +dtsBrandDto);
        Page page = PageHelper.startPage(dtsBrandDto.getPage(),dtsBrandDto.getLimit());

        List<DtsBrand> categroys=mapper.findPagebrand(dtsBrandDto);
        System.out.println("查询结果: " + categroys); // 打印查询结果
        System.out.println("分页结果: " + page.getResult()); // 打印分页结果
        Map map = new HashMap();
        map.put("list", page.getResult());
        map.put("total", page.getTotal());
        map.put("totalPage", page.getPages());
        map.put("pageNum", page.getPageNum());
        map.put("pageSize", page.getPageSize());
        return map;
    }

    @Override
    public boolean branddelete(String ids) {
        return mapper.branddelete(ids);
    }
    @Override
    public Integer updatebrand(Map map) {
        return  mapper.updatebrand(map);
    }

    @Override
    public Integer addbrand(Map map) {
        return  mapper.addbrand(map);
    }
}
